package com.jerry.singlethreadexecution;
//将左右餐具封装到同一个餐具对里，并且在用餐时加锁，可以保证一个人在用餐结束完成后，后面的人才可以使用，。保证餐具同时只能被一个线程使用。避免死锁
public class EatNoodleThread extends  Thread {
    private final TableWarePair tableWarePair;
    private final String name;
    public EatNoodleThread(String name, TableWarePair tableWarePair) {
        this.tableWarePair = tableWarePair;
        this.name = name;
    }

    @Override
    public void run() {
        while(true){

         this.eat();

        }
    }

    private void eat(){

        synchronized (tableWarePair){

            System.out.println(name + " take up " + tableWarePair.getLeftTableWare()+"(left)");
            System.out.println(name + " take up " + tableWarePair.getRightTableWare()+"(right)");
            System.out.println(name + "is eating now");
            System.out.println(name + " put down " + tableWarePair.getRightTableWare()+"(right)");
            System.out.println(name + " put down " + tableWarePair.getLeftTableWare()+"(left)");
        }

    }

    public static void main(String[] args) {

    }
}
